package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.Finish1;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * ---发票类型
 */
public class InvoiceTypeActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private ImageView iv_choose1, iv_choose2;

    private String from;
    private final int FROM_INVOICE_TYPE = 643;
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Finish1 event) {
        finish();
    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);//反注册EventBus
        super.onDestroy();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invoice_type);

        EventBus.getDefault().register(this);
      //  MyApplication.getInstance().addDestoryActivity(this,"addform2");
        initData();
        initView();
    }

    private void initData() {
        from = getIntent().getStringExtra("from");
    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("发票类型");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.rl_common).setOnClickListener(this);
        findViewById(R.id.rl_dedicated).setOnClickListener(this);

        iv_choose1 = (ImageView) findViewById(R.id.iv_choose1);
        iv_choose2 = (ImageView) findViewById(R.id.iv_choose2);
    }

    private String type = "1";

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //普通类型
            case R.id.rl_common:
                iv_choose1.setVisibility(View.VISIBLE);
                iv_choose2.setVisibility(View.GONE);

                if ("Tax".equals(from)) {
                    if (null != TaxInvoiceActivity.mActivity) {
                        TaxInvoiceActivity.mActivity.finish();
                        InvoiceTypeActivity.this.finish();
                    }
                }
                InvoiceTypeActivity.this.finish();

                break;

            //增值税专用发票
            case R.id.rl_dedicated:
                iv_choose1.setVisibility(View.GONE);
                iv_choose2.setVisibility(View.VISIBLE);
                intent = new Intent(this, TaxInvoiceActivity.class);
                intent.putExtra("typeName", "增值税专用发票");
                intent.putExtra("type", "3");
                startActivityForResult(intent, FROM_INVOICE_TYPE);
                break;

            default:
                break;
        }
    }

    private String typeName;
    private String type1;
    private String invoiceTitle;
    private String taxpayerId;
    private String bank;
    private String bankAccount;
    private String address;
    private String phone;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case FROM_INVOICE_TYPE:
                if (resultCode == RESULT_OK) {
                    typeName = data.getExtras().getString("result");
                    type1 = data.getExtras().getString("type1");
                    invoiceTitle = data.getExtras().getString("invoiceTitle");
                    taxpayerId = data.getExtras().getString("taxpayerId");
                    bank = data.getExtras().getString("bank");
                    bankAccount = data.getExtras().getString("bankAccount");
                    address = data.getExtras().getString("address");
                    phone = data.getExtras().getString("phone");
                    Intent intent = new Intent();
                    intent.putExtra("result", typeName);
                    intent.putExtra("type1", type1);
                    intent.putExtra("invoiceTitle", invoiceTitle);
                    intent.putExtra("taxpayerId", taxpayerId);
                    intent.putExtra("bank", bank);
                    intent.putExtra("bankAccount", bankAccount);
                    intent.putExtra("address", address);
                    intent.putExtra("phone", phone);
                    InvoiceTypeActivity.this.setResult(RESULT_OK, intent);
                    InvoiceTypeActivity.this.finish();
                }
                break;

            default:
                break;
        }
    }
}
